package cn.wen.bases.framework.struts.interceptor;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import cn.wen.bases.framework.struts.UtilsOfStruts;
import cn.wen.java.lang.HelperOfString;
import cn.wen.java.nio.Charset;
import cn.wen.javax.servlet.http.UtilsOfRequest;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * Title：请求编码转换拦截器
 * <p>
 * Description：
 * <p>
 * Copyright：Copyright (c) 2010-2012
 * 
 * @author <a href="mailto:ct_wen@qq.com">ctwen</a>, 温承瑭
 * @version Revision：1.0 Date：2010/11/15 Description：初完成。
 *          <p>
 */
public class RequestEncodeInterceptor extends AbstractInterceptor {

    private static final long serialVersionUID = 6716272566399172036L;
    
    private Logger log = Logger.getLogger(getClass());

    /**
     * <pre>
     * HttpServletRequest编码转换。
     * </pre>
     * 
     * @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
     */
    @Override
    public String intercept(ActionInvocation ai) throws Exception {
	HttpServletRequest request = UtilsOfStruts.getHttpServletRequest();
	String method = request.getMethod();

	if (method.equalsIgnoreCase("get")) {
	    UtilsOfRequest.convertEncode(request, Charset.ISO_8859_1, Charset.UTF_8);
	}
	else if (method.equalsIgnoreCase("post")) {
	    String qs = request.getQueryString();
	    if (qs != null) {
		qs = new String(qs.getBytes(Charset.ISO_8859_1), Charset.UTF_8);
		UtilsOfRequest.convertPostUrlEncode(request, Charset.ISO_8859_1, Charset.UTF_8, qs);
	    }
	}
	else {
	    log.info("mothed is not get or post");
	}
	log.debug(HelperOfString.format("{2}方式：从编码：\"{0}\" 转换到：\"{1}\"编码处理。", Charset.ISO_8859_1, Charset.UTF_8,
		method));
	return ai.invoke();
    }

}
